using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;
namespace Data
{
    public class KhachHangRepository
    {



		//////////////////////////////////////////////////////////

		                                                          

		//            Enter code your here                      //


        //            kiem tra khach nuopc ngoai                     //
        public bool KiemTraKhachNuocNgoai(KhachHangDTO obj)
        {
            if (obj.MaLoaiKhachHang == LoaiKhachHangDTO.MaKhachNuocNgoai)
                return true;
            return false;
        }
        public bool KiemTraKhachNuocNgoai(List<KhachHangDTO> lst)
        {
            for (int i = 0; i < lst.Count; i++)
            {
                if (this.KiemTraKhachNuocNgoai(lst[i]) == true)
                    return true;
            }
            return false;
        }
		                                                          

		                                                                      

		//////////////////////////////////////////////////////////

        public static KhachHangDTO CreateKhachHangDTOFromKhachHangEF(KhachHang objEf) // EF -> DTO
        {
            KhachHangDTO objDTO = new KhachHangDTO();
            Mapper.CreateMap<KhachHang, KhachHangDTO>();
            objDTO = Mapper.Map<KhachHang, KhachHangDTO>(objEf);
            return objDTO;
        }

        public static KhachHang CreateKhachHangEFFromKhachHangDTO(KhachHangDTO objDTO)    //DTO -> EF
        {
            KhachHang objEF = new KhachHang();
            Mapper.CreateMap<KhachHangDTO, KhachHang>();
            objEF = Mapper.Map<KhachHangDTO, KhachHang>(objDTO);
            return objEF;
        }


        public static int Count()  //Dem so luong cac bo du lieu trong bang
        {
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            int qr = (from x in Context.KhachHangList
                      select x.MaKhachHang).Count();
            return qr;
        }
        public int GetIdentity() // tra ve gia tri ma moi them vo( gia tri Ma lon nhat trong Bang).
        {
            try
            {
                 QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
                  var query = from x in Context.KhachHangList
                        orderby x.MaKhachHang descending
                        select x.MaKhachHang;
                  var Ma = query.First();
                  return int.Parse(Ma.ToString());
            }
            catch
            {
                return 0;
            }
        }

        public bool Them(KhachHangDTO objDTO)
        {
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                KhachHang objEF = CreateKhachHangEFFromKhachHangDTO(objDTO);
                context.AddToKhachHangList(objEF);
                context.SaveChanges();
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

        public void CapNhat(KhachHangDTO objDTO)
        {
            try
            {
                KhachHang objEF = CreateKhachHangEFFromKhachHangDTO(objDTO);
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                context.KhachHangList.ApplyCurrentValues(objEF);
                context.SaveChanges();
            }
            catch (System.Exception )
            {            	
            }
        }

        public List<KhachHangDTO> LayDanhSach(int Start, int Record)//lay Record bo du lieu tu vi tri Start
        { 
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            var query = (from x in Context.KhachHangList
                        orderby x.MaKhachHang
                        select x).Skip(Start - 1).Take(Record);
            List<KhachHangDTO> lst = new List<KhachHangDTO>();
            foreach (var obj in query)
            {
                KhachHangDTO lp = KhachHangRepository.CreateKhachHangDTOFromKhachHangEF(obj);
                lst.Add(lp);
            }
            return lst;
        }

        public List<KhachHangDTO> LayDanhSach()//lay tat ca danh sach
        {
            List<KhachHangDTO> lst = new List<KhachHangDTO>();
            QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
            var query = from KhachHang in context.KhachHangList
                        select KhachHang;
            foreach (var objEf in query)
            {
                KhachHangDTO objDTO = CreateKhachHangDTOFromKhachHangEF(objEf);
                lst.Add(objDTO);
            }
            return lst;
        }

        public KhachHangDTO LayTheoMa(int MaKhachHang)
        {
            KhachHangDTO objDTO = new KhachHangDTO();
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                var query = from KhachHang in context.KhachHangList
                            where KhachHang.MaKhachHang == MaKhachHang
                            select KhachHang;
                KhachHang objEF = query.First();
                objDTO = CreateKhachHangDTOFromKhachHangEF(objEF);
            }
            catch (System.Exception )
            {
            }
            return objDTO;
        }

//
//Khoa ngoai LoaiKhachHang
//
        public List<KhachHangDTO> LayDanhSachTheoMaLoaiKhachHang( int MaLoaiKhachHang)
        {
            List<KhachHangDTO> lst = new List<KhachHangDTO>();
            try
            {
                 QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
                 var query = from KhachHang in context.KhachHangList
                         where KhachHang.MaLoaiKhachHang == MaLoaiKhachHang 
                        select KhachHang;
                 foreach (var objEf in query)
                 {
                      KhachHangDTO objDTO = CreateKhachHangDTOFromKhachHangEF(objEf);
                       lst.Add(objDTO);
                 }
            }
            catch
            {}
            return lst;
        }



        public void XoaTheoMaLoaiKhachHang( int MaLoaiKhachHang, bool XoaKhoaNgoai=false)
        {
            List<KhachHangDTO> lst = this.LayDanhSachTheoMaLoaiKhachHang( MaLoaiKhachHang);
            for( int i = 0; i < lst.Count; i++ )
            {
                 int iMa = lst[i].MaKhachHang;
                 this.Xoa(iMa, XoaKhoaNgoai);
            }
        }

        public bool Xoa(int MaKhachHang, bool XoaKhoaNgoai=false)
        {
            try
            {
                if( XoaKhoaNgoai == true)
                {
                
                       ChiTietPhieuThueRepository ChiTietPhieuThueRepo = new ChiTietPhieuThueRepository();
                       ChiTietPhieuThueRepo.XoaTheoMaKhachHang(MaKhachHang, XoaKhoaNgoai);

                       HoaDonRepository HoaDonRepo = new HoaDonRepository();
                       HoaDonRepo.XoaTheoMaKhachHang(MaKhachHang, XoaKhoaNgoai);

                }
                QuanLyKhachSanEntities context = new QuanLyKhachSanEntities();
                var query = from x in context.KhachHangList
                            where x.MaKhachHang == MaKhachHang 
                            select x;
                if(query.First()!= null)
                {
                    context.DeleteObject(query.First());
                    context.SaveChanges();
                }
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

////////////////////////////////////////////////////////////////

    }
}

